package cn.zhuangcloud.newversion.common;

import cn.zhuangcloud.newversion.common.model._MappingKit;
import cn.zhuangcloud.newversion.common.validator.TokenValidator;
import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.util.JdbcConstants;
import com.alibaba.druid.wall.WallConfig;
import com.alibaba.druid.wall.WallFilter;
import com.jfinal.config.*;
import com.jfinal.core.JFinal;
import com.jfinal.json.MixedJsonFactory;
import com.jfinal.kit.LogKit;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.dialect.PostgreSqlDialect;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.redis.RedisPlugin;
import com.jfinal.render.ViewType;
import com.jfinal.template.Engine;

public class Config extends JFinalConfig {

    private static final Prop p = loadConfig();
    private static WallFilter wallFilter;

    private static Prop loadConfig() {
        try {
            return PropKit.use("config-dev.properties");
        } catch (RuntimeException e) {
            LogKit.logNothing(e);
            return PropKit.use("config.properties");
        }
    }

    public static void main(String[] args) {
        JFinal.start("src/main/webapp", 80, "/");
    }

    public static DruidPlugin getDruidPlugin() {
        //*******************************
        DruidPlugin dp = new DruidPlugin(p.get("postgres.url"), p.get("postgres.username"), p.get("postgres.password"), p.get("postgres.driverClass"));
        if (null == wallFilter)
            wallFilter = new WallFilter();
        //*************************
        //wallFilter.setDbType(JdbcConstants.SQL_SERVER);
        wallFilter.setDbType(JdbcConstants.POSTGRESQL);
        StatFilter statFilter = new StatFilter();
        dp.addFilter(wallFilter);
        dp.addFilter(statFilter);
        return dp;
    }

    @Override
    public void configConstant(Constants me) {
        me.setDevMode(true);
        me.setJsonFactory(new MixedJsonFactory());
        me.setViewType(ViewType.JFINAL_TEMPLATE);
    }

    @Override
    public void configRoute(Routes me) {
        me.add(new FrontRoutes());
    }

    @Override
    public void configEngine(Engine me) {
        me.addSharedFunction("view/_layout.html");
        //me.addDirective("random", new RandomDirective());
        //me.addSharedStaticMethod(StrKit.class);//#(TokenValidator::Validate(token))
    }

    private ActiveRecordPlugin getActiveRecordPlugin(DruidPlugin dp) {
        ActiveRecordPlugin arp = new ActiveRecordPlugin(dp);
        arp.setDialect(new PostgreSqlDialect());
        _MappingKit.mapping(arp);
        arp.setBaseSqlTemplatePath(PathKit.getWebRootPath() + "/WEB-INF/sql");
        arp.addSqlTemplate("all.sql");
        return arp;
    }

    private RedisPlugin getRedisPlugin() {
        return new RedisPlugin("token", p.get("redis.host"), p.getInt("redis.port"), p.getInt("redis.timeOut"), p.get("redis.password"), p.getInt("redis.database.token"));
    }

    @Override
    public void configPlugin(Plugins me) {
        DruidPlugin dp = getDruidPlugin();
        me.add(dp);
        me.add(getActiveRecordPlugin(dp));
        me.add(getRedisPlugin());
    }

    @Override
    public void configInterceptor(Interceptors me) {
        me.addGlobalActionInterceptor(new TokenValidator());
    }

    @Override
    public void configHandler(Handlers me) {

    }

    @Override
    public void afterJFinalStart() {
        if (null != wallFilter) {
            WallConfig wallConfig = wallFilter.getConfig();
            wallConfig.setSelectUnionCheck(false);
            wallConfig.setMultiStatementAllow(true);
            wallConfig.setNoneBaseStatementAllow(true);
            wallConfig.setMergeAllow(true);
        }
    }
}
